﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace _4个红外测温仪
{
    public partial class Form3 : Form
    {
        public int HistoryDate = 0;
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
        public Form3()
        {
            InitializeComponent();
            this.dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
        }
        public void HistoryCSVDateShow()//参数读取
        {
            //****//绝对地址// string csvFile = System.IO.Path.Combine(Application.StartupPath, @"C:\Users\Administrator\Desktop\log.csv");//************************************///这里是历史数据保存的地址，随便改啊//************************************//
            //string csvFile = System.IO.Path.Combine(Application.StartupPath, Path.GetFullPath(txtPath.Text));
           
            string csvFile = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Path.GetFullPath(txtPath.Text));
            List<string[]> rows = File.ReadAllLines(csvFile).Select(x => x.Split(',')).ToList();
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("6号温度");
            dataTable.Columns.Add("7号温度");
            dataTable.Columns.Add("8号温度");
            dataTable.Columns.Add("9号温度");
            dataTable.Columns.Add("日期");
            dataTable.Columns.Add("备注");
            rows.ForEach(x => { dataTable.Rows.Add(x); });
            dataGridView1.DataSource = dataTable;

            long csvSize;
            string filePath = csvFile;
            FileInfo fileInfo = new FileInfo(filePath);
            csvSize = fileInfo.Length / 1024;
            /*if (csvSize >= 1024*2)
            {
                MessageBox.Show("历史数据超过2M了，记得及时转移和清理！");
            }*/
            label27.Text = csvSize.ToString();
        }
        private void Form3_Load(object sender, EventArgs e)
        {
            LoadFile(System.AppDomain.CurrentDomain.BaseDirectory + "历史数据", treeView1.Nodes);
        }
        public static void LoadFile(string path, TreeNodeCollection treeNodeCollection)  // 递归加载目录和文件道树视图中
        {
            string[] directories = null;
            try
            {
                directories = Directory.GetDirectories(path);
            }
            catch (Exception)
            {
                return;
            }
            foreach (var item in directories)
            {
                LoadFile(item, treeNodeCollection.Add(Path.GetFileName(item)).Nodes);
            }
            string[] files = null;
            try
            {
                files = Directory.GetFiles(path);
            }
            catch (Exception)
            {
                return;
            }
            foreach (var item in files)
            {
                treeNodeCollection.Add(Path.GetFileName(item));
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            label28.Text = dateTimePicker1.Text + "历史数据KB大小：";
            if (HistoryDate == 0)
            {
                button1.Text = "清空";
                HistoryDate = 1;
                try
                { HistoryCSVDateShow();
                    
                }
                catch
                {
                    MessageBox.Show("被选日期的历史数据不存在！");
                    DataTable dataTableErr = new DataTable();
                    dataTableErr.Columns.Add("被选日期的历史数据不存在！");
                    dataTableErr.Rows.Add("被选日期的历史数据不存在！");
                    dataGridView1.DataSource = dataTableErr;
                    label27.Text = "文件找不到";
                }
            }
            else
            {
                button1.Text = "加载";
                HistoryDate = 0;
                dataGridView1.DataSource = null;
                dataGridView1.Rows.Clear();
                dataGridView1.Columns.Clear();
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            txtPath.Text = System.AppDomain.CurrentDomain.BaseDirectory + "历史数据\\" +  treeView1.SelectedNode.Text;
           
        }

        private void txtPath_TextChanged(object sender, EventArgs e)
        {

        }

        private void label27_Click(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            txtPath.Text = System.AppDomain.CurrentDomain.BaseDirectory + "历史数据\\" + "温度数据" + dateTimePicker1.Text + ".csv";
        }
    }
}